package com.unbank.util;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.unbank.bhi.BhiConsole;

/**
 * 
 * @author 梁杨桃
 * */
public class JsoupUtil {
	private static Log logger = LogFactory.getLog(JsoupUtil.class);

	public static Document readUrl(String url) {
		if (url == null || url.trim().isEmpty()) {
			return null;
		}
		Document doc = null;
		Connection conn = null;
		try {
			conn = Jsoup.connect(url);
			conn.header(
					"User-Agent",
					"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER");
			conn.header("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			conn.header("Accept-Encoding", "gzip,deflate,sdch");
			conn.header("Accept-Language", "zh-CN,zh;q=0.8");
			conn.header("Connection", "keep-alive");
			doc = conn.timeout(60 * 1000).get();
		} catch (Exception e) {
			logger.error("读取Url" + url + "失败" + e);
			try {
				doc = conn.timeout(60 * 1000).post();
			} catch (IOException e1) {
				logger.error("读取Url" + url + "失败" + e1);

			}

		}
		return doc;
	}

	public static boolean checkURL(String url) {
		if (url == null || url.trim().isEmpty()) {
			return false;
		}
		Document doc = null;
		Connection conn = null;
		try {
			conn = Jsoup.connect(url);
			conn.header(
					"User-Agent",
					"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER");
			conn.header("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			conn.header("Accept-Encoding", "gzip,deflate,sdch");
			conn.header("Accept-Language", "zh-CN,zh;q=0.8");
			conn.header("Connection", "keep-alive");
			doc = conn.timeout(60 * 1000).get();
		} catch (Exception e) {
			logger.error("读取Url" + url + "失败" + e);
			try {
				doc = conn.timeout(60 * 1000).post();
			} catch (IOException e1) {
				logger.error("读取Url" + url + "失败" + e1);
				return false;
			}

		}
		if (doc == null) {
			return false;
		} else {
			return true;
		}
	}

	public static String crawlerContentByTag(String url, Document document,
			String content_tag, int page) {
		// 如果父标签为空
		if (content_tag == null || content_tag.trim().isEmpty()) {
			return null;
		}
		// 如果内容包含 全文阅读的标签
		if (document.text().contains("全文阅读")) {
			Elements elements = document.getElementsContainingOwnText("全文阅读");
			for (Element element : elements) {
				if (element.attr("href") != null) {
					url = UrlTools.getFullUrl(url, element.attr("href").trim());
					Document documentQuan = readUrl(url);
					if (documentQuan == null) {

					} else {
						document = documentQuan;
					}
				}
			}

		}

		// 如果有多个标签
		StringBuffer contentStr = new StringBuffer();

		String[] tags = content_tag.split("@@@");
		// 的内容
		String content = null;
		for (String tag : tags) {
			if (tag.trim().isEmpty()) {
				continue;
			}
			String[] string = tag.split(" > ");
			if (string.length == 2) {
				content = extractContent(document, string[0]);
				Document document2 = Jsoup.parse(content);
				contentStr.append(extractContent(document2, string[1]));
			} else {

				content = extractContent(document, tag);
				contentStr.append(content);
			}

		}
		// System.out.println(contentStr.toString());
		if (page == 1) {
			Document pageDocument = Jsoup.parse(content);
			Elements elements = pageDocument.select("a");
			for (Element element : elements) {

				String nextTxt = element.text().trim();
				if ((isNumeric(nextTxt) && Integer.parseInt(nextTxt) > page)) {
					String nextPage = UrlTools.getFullUrl(url,
							element.attr("href").trim());
					Document document3 = JsoupUtil.readUrl(nextPage);

					if (document3 != null) {
						page++;
						String temp = crawlerContentByTag(url, document3,
								content_tag, page);
						contentStr.append(temp);

					}
				}
			}
		}
		String html = contentStr.toString();
		html = html.replaceAll(" href=\"\\S[^\"]*\"", " ");
		html = html.replaceAll(" target=\"\\S[^\"]*\"", " ");
		// html = html.replaceAll("(?is)<a.*?>.*?</a>", "");
		return html;

	}

	// 根据标签 和 Document 获得新闻内容
	public static String extractContent(Document document, String content_tag) {
		Elements contentElements = null;
		StringBuffer sb = new StringBuffer();
		contentElements = document.select(content_tag);
		if (contentElements != null && contentElements.size() != 0) {
			for (Element element : contentElements) {
				sb.append(element);
			}
		}
		String content = sb.toString();
		content = content.replaceAll("(?is)<!--.*?-->", "");
		content = content.replaceAll("(?is)<textarea.*?>.*?</textarea>", "");
		content = content.replaceAll("(?is)<script.*?>.*?</script>", "");
		content = content.replaceAll("(?is)<style.*?>.*?</style>", "");
		content = content.replaceAll("(?is)<option.*?>.*?</option>", "");
		content = content.replaceAll("(?is)<meta.*?>.*?</meta>", "");
		content = content.replaceAll("(?is)<link.*?>.*?</link>", "");
		content = content.replaceAll("(?is)<noscript.*?>.*?</noscript>", "");
		content = content.replaceAll("(?is)<param.*?>.*?</param>", "");
		content = content.replaceAll("(?is)<iframe.*?>.*?</iframe>", "");
		content = content.replaceAll("&.{2,8};|&#.{2,8};", "");
		content = content.replaceAll("\\?", "");
		content = content.replaceAll(
				"<(?!\\/?(br|p|table|tbody|tr|th|td|a)).*?>", "");
		content = content.replaceAll(" id=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" class=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" name=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" height=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" width=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" border=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" alt=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" style=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" align=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" script=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" language=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" src=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellspacing=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellpadding=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" colspan=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" bgcolor=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" valign=\"\\S[^\"]*\"", " ");
		if (content.length() > 20) {
			return content;
		} else {
			return "";
		}
	}

	public static boolean isNumeric(String s) {
		// 正则表达式

		if (s.trim().isEmpty()) {
			return false;
		}

		return (s.matches("\\d*") && Pattern.compile("[0-9]*").matcher(s)
				.matches());
	}

	// 获取新闻内容
	public static String extractContent(Document document) {
		String content = document.toString().toLowerCase();
		content = content.replaceAll("(?is)<!--.*?-->", "");
		content = content.replaceAll("(?is)<textarea.*?>.*?</textarea>", "");
		content = content.replaceAll("(?is)<script.*?>.*?</script>", "");
		content = content.replaceAll("(?is)<style.*?>.*?</style>", "");
		content = content.replaceAll("(?is)<option.*?>.*?</option>", "");
		content = content.replaceAll("(?is)<meta.*?>.*?</meta>", "");
		content = content.replaceAll("(?is)<link.*?>.*?</link>", "");
		content = content.replaceAll("(?is)<noscript.*?>.*?</noscript>", "");
		content = content.replaceAll("(?is)<param.*?>.*?</param>", "");
		content = content.replaceAll("(?is)<iframe.*?>.*?</iframe>", "");
		content = content.replaceAll("(?is)<a.*?>.*?</a>", "");
		content = content.replaceAll("&.{2,5};|&#.{2,5};", "");
		content = content.replaceAll("\\?", "");
		content = content.replaceAll(
				"<(?!\\/?(br|p|table|tbody|tr|th|td)).*?>", "");
		content = content.replaceAll(" id=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" class=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" name=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" height=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" width=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" border=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" alt=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" style=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" align=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" script=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" language=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" src=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellspacing=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellpadding=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" colspan=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" bgcolor=\"\\S[^\"]*\"", " ");
		content = content.replace("|", "");
		content = content.replace("｜", "");
		content = content.replace("-", "");
		content = content.replace("	", "");
		String[] eleList = content.split("\n");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < eleList.length - 4; i++) {
			String string = eleList[i].trim();
			if (string.trim().length() > 20) {

				if (string.contains("版权所有") || string.contains("版 权 所 有")
						|| string.contains("热线") || string.contains("转载")
						|| string.contains("copyright")
						|| StringUtil.isRegex(string, "备\\d{5,20}号")) {
					break;
				}
				if (string.contains("></p>")) {
					if (!StringUtil.isRegex(string, "[\u4e00-\u9fa5]+")) {
						continue;
					}
				}
				sb.append(string);

			}
		}
		content = sb.toString();
		return content = content.replace("	", "");

	}

	public static String crawlerTimeByTag(Document document, String content_tag) {
		String newstime = null;
		String[] tags = content_tag.split("@@@");
		StringBuffer sb = new StringBuffer();
		Elements contentElements = null;
		for (String string : tags) {
			contentElements = document.select(string);
			if (contentElements != null && contentElements.size() != 0) {
				for (Element element : contentElements) {
					sb.append(element);
				}
			}
		}
		if (contentElements == null || contentElements.size() == 0) {
			return null;
		}
		String content = sb.toString();
		String time = StringUtil.getStringByReg(content.trim(),
				"[\\d]+.[\\d]+.[\\d]+日|[\\d]+.[\\d]+.[\\d]+");
		newstime = StringUtil.dateToString(StringUtil.stringToDate(time));
		return newstime;

	}

	public static String crawlerContentByTag(String url, Document document,
			String content_tag) {
		return crawlerContentByTag(url, document, content_tag, 1);
	}

}
